import { transactionDataObject } from './transactionDataObject';
import { enterpriseDataObject } from './enterpriseDataObject';
import { proposeDataObject } from './proposeDataObject';
import { noticeDataObject } from './noticeDataObject';
import { userDataObject } from './userDataObject';
// @ts-ignore
import * as schema from './app-schema.json';
import { AGConnectCloudDB, CloudDBZone, CloudDBZoneQuery } from '@hw-agconnect/database-ohos';
import { AGCRoutePolicy } from '@hw-agconnect/core-ohos';
import { getAGConnect } from './AgcConfig';
export class CloudDBService {
    private static ZONE_NAME: string = "cloudDBZoneUserData"
    private static init(context: any): Promise<CloudDBZone> {
        return new Promise((resolve, reject) => {
            // 获取AGC连接
            getAGConnect(context);
            AGConnectCloudDB.initialize(context);
            AGConnectCloudDB.getInstance({
                context: context,
                agcRoutePolicy: AGCRoutePolicy.CHINA,
                objectTypeInfo: schema
            }).then((ret) => {
                return resolve(ret.openCloudDBZone(this.ZONE_NAME));
            }).catch((err) => {
                return reject(err);
            });
        })
    }
//所有人
    //获取数据
    public static query(context: any,UserName : string ): Promise<userDataObject> {
        return new Promise((resolve, reject) => {
            const query = CloudDBZoneQuery.where(userDataObject).equalTo("UserName",UserName);
            this.init(context).then((ret) => {
                ret.executeQuery(query).then((ret) => {
                    console.log('xxx'+ret)
                    resolve(ret.getSnapshotObjects()[0]);
                })
            }).catch((err) => {
                console.log('xxx'+err)
                reject(err);
            });
        })
    }
    //批量获取数据
    public static queryUserData(context: any,Village : string ): Promise<Array<userDataObject>> {

        return new Promise((resolve, reject) => {
            const query = CloudDBZoneQuery.where(userDataObject).equalTo("Village",Village);
            this.init(context).then((ret) => {
                ret.executeQuery(query).then((ret) => {
                    //
                    resolve(ret.getSnapshotObjects());
                })
            }).catch((err) => {
                console.error('xx '+ '获取通知'+JSON.stringify(err))
                reject(err);
            });
        })
    }
    //插入单条数目
    public static insert(context: any, object: userDataObject): Promise<number> {
        return new Promise((resolve, reject) => {
            this.init(context).then((ret) => {
                ret.executeUpsert(object).then((res) => {
                    resolve(res)
                })
            }).catch((err) => {
                console.warn('xx 3 ' + JSON.stringify(err))
                reject(err);
            });
        })
    }
//    删除数据
    public static delete(context: any, object: userDataObject): Promise<number> {
        return new Promise((resolve, reject) => {
            this.init(context).then((ret) => {
                ret.executeDelete(object).then((res) => {
                    resolve(res)
                })
            }).catch((err) => {
                console.warn('xx 3 ' + JSON.stringify(err))
                reject(err);
            });
        })
    }

//    获取通知
    //根据所在街道获取数据
    public static queryNotice(context: any,Village : string ): Promise<Array<noticeDataObject>> {

        return new Promise((resolve, reject) => {
            const query = CloudDBZoneQuery.where(noticeDataObject).equalTo("Village",Village);
            this.init(context).then((ret) => {
                ret.executeQuery(query).then((ret) => {
//
                    resolve(ret.getSnapshotObjects());
                })
            }).catch((err) => {
                console.error('xx '+ '获取通知'+JSON.stringify(err))
                console.info('xx ' + Village )
                reject(err);
            });
        })
    }
    //插入单条数目
    public static insertNotice(context: any, object: noticeDataObject): Promise<number> {
        return new Promise((resolve, reject) => {
            this.init(context).then((ret) => {
                ret.executeUpsert(object).then((res) => {
                    resolve(res)
                })
            }).catch((err) => {
                console.warn('xx' + JSON.stringify(err))
                reject(err);
            });
        })
    }

    //    获取建议
    //获取数据
    public static queryPropose(context: any,Village : string ): Promise<Array<proposeDataObject>> {

        return new Promise((resolve, reject) => {
            const query = CloudDBZoneQuery.where(proposeDataObject).equalTo("Village",Village);
            this.init(context).then((ret) => {
                ret.executeQuery(query).then((ret) => {
                    console.log('xx ' +'获取建议'+ JSON.stringify(ret))
                    resolve(ret.getSnapshotObjects());



                })
            }).catch((err) => {
                console.error('xx '  +'获取建议'+ JSON.stringify(err))
                console.info('xx ' + Village )
                reject(err);

            });
        })
    }
    //插入单条数目
    public static insertPropose(context: any, object: proposeDataObject): Promise<number> {
        return new Promise((resolve, reject) => {
            this.init(context).then((ret) => {
                ret.executeUpsert(object).then((res) => {
                    resolve(res)
                })
            }).catch((err) => {
                console.warn('xx 3 ' + JSON.stringify(err))
                reject(err);
            });
        })
    }

//    访问Enterprise
    //获取全部数据
    public static queryEnterprise(context: any): Promise<Array<enterpriseDataObject>> {

        return new Promise((resolve, reject) => {
            const query = CloudDBZoneQuery.where(enterpriseDataObject)
            this.init(context).then((ret) => {
                ret.executeQuery(query).then((ret) => {
                    console.log('xx ' +'获取建议'+ JSON.stringify(ret))
                    resolve(ret.getSnapshotObjects());
                })
            }).catch((err) => {
                console.error('xx '  +'获取建议'+ JSON.stringify(err))
                reject(err);

            });
        })
    }
    //按条件获取数据
    //获取数据
    public static queryEnterprisePlus(context: any,UserName : string ): Promise<Array<enterpriseDataObject>> {

        return new Promise((resolve, reject) => {
            const query = CloudDBZoneQuery.where(enterpriseDataObject).equalTo("UserName",UserName);
            this.init(context).then((ret) => {
                ret.executeQuery(query).then((ret) => {
                    console.log('xx ' +'获取建议'+ JSON.stringify(ret))
                    resolve(ret.getSnapshotObjects());
                })
            }).catch((err) => {
                console.error('xx '  +'获取建议'+ JSON.stringify(err))
                reject(err);
            });
        })
    }
    //插入单条数目
    public static insertEnterprise(context: any, object: enterpriseDataObject): Promise<number> {
        return new Promise((resolve, reject) => {
            this.init(context).then((ret) => {
                ret.executeUpsert(object).then((res) => {
                    resolve(res)
                })
            }).catch((err) => {
                console.warn('xx 3 ' + JSON.stringify(err))
                reject(err);
            });
        })
    }
//    访问transaction
    public static queryTransaction(context: any,Village : string ): Promise<Array<transactionDataObject>> {
        return new Promise((resolve, reject) => {
            const query = CloudDBZoneQuery.where(transactionDataObject).equalTo("Village",Village);
            this.init(context).then((ret) => {
                ret.executeQuery(query).then((ret) => {
                    resolve(ret.getSnapshotObjects());
                })
            }).catch((err) => {
                reject(err);
            });
        })
    }
    //插入单条数目
    public static insertTransaction(context: any, object: transactionDataObject): Promise<number> {
        return new Promise((resolve, reject) => {
            this.init(context).then((ret) => {
                ret.executeUpsert(object).then((res) => {
                    resolve(res)
                })
            }).catch((err) => {
                console.warn('xx 3 ' + JSON.stringify(err))
                reject(err);
            });
        })
    }
}